<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <el-button type="primary" size="small" @click="openDialog(1, 0)">
          添加权限
        </el-button>
      </page-tools>
      <!-- ⭐树形表格，数据必须树形的上下级结构的，下级数据要放在children里 -->
      <!-- ⭐必须添加row-key唯一字段名 -->
      <el-table row-key="id" :data="list" border>
        <el-table-column prop="name" align="center" label="名称" />
        <el-table-column prop="code" align="center" label="标识" />
        <el-table-column prop="description" align="center" label="描述" />
        <el-table-column align="center" label="操作">
          <template v-slot="scope">
            <el-button
              v-if="scope.row.type === 1"
              @click="openDialog(2, scope.row.id)"
              type="text"
            >
              添加
            </el-button>
            <el-button type="text" @click="editRow(scope.row.id)">
              编辑
            </el-button>
            <el-button type="text" @click="delRow(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <AddPerm ref="addPerm" @updateList="initData"></AddPerm>
    </div>
  </div>
</template>

<script>
import { getPermissiontListApi, delPermissiontApi } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
import AddPerm from './components/addPerm.vue'
export default {
  name: 'Permission',
  components: {
    AddPerm
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.initData()
  },
  methods: {
    // ⭐获取权限列表
    async initData() {
      const res = await getPermissiontListApi()
      this.list = tranListToTreeData(res, '0')
    },
    openDialog(type, pid) {
      // console.log(type, pid)
      this.$refs.addPerm.dialogShow = true
      //
      this.$refs.addPerm.formData.type = type
      this.$refs.addPerm.formData.pid = pid
    },
    editRow(id) {
      this.$refs.addPerm.getRowInfo(id)
    },
    async delRow(id) {
      try {
        await this.$confirm('确认删除吗？')
      } catch (error) {
        return
      }
      await delPermissiontApi(id)
      this.$message.success('删除成功')
      this.initData()
    }
  }
}
</script>

<style></style>
